
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.vmware.vim25.HostSnmpConfigSpec;
import com.vmware.vim25.HostSnmpDestination;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.OptionValue;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.VirtualMachineConfigSpec;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostSnmpSystem;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;

public class ConfigureSnmpSystem

{

	private static final String ipaddress = "";
    private static final String URL_VCENTER = "https://"+ipaddress+"/sdk";
    private static final String username = "Admin";
    private static final String password = "passwd";
    
    
    public static void  test(){
    	
    	
    	 try {
    		 
			ServiceInstance si = new ServiceInstance(new URL(URL_VCENTER), username,
			            password, true);
			
			System.out.println(si.getAboutInfo().getOsType());
			
			System.out.println(si.getEventManager().getLatestEvent().fullFormattedMessage);
			
//			si.getRootFolder().getChildEntity()[0].getMOR();
//			
//			VirtualMachine machine = new VirtualMachine(si.getServerConnection(), new ManagedObjectReference());
//			
//			System.out.println(machine.getConfig().getCpuAllocation().limit);
//			
			ManagedEntity[] ms = si.getRootFolder().getChildEntity();
			
			VirtualMachine machine2 = null;
			
			VirtualMachineConfigInfo configInfo = null;
			
			for(ManagedEntity entity : ms){
				
				machine2 = new VirtualMachine(si.getServerConnection(), entity.getMOR());
				
			    configInfo = machine2.getConfig();
				
				System.out.println(configInfo.cpuAffinity.toString());
				
				System.out.println(configInfo.getMemoryAllocation().limit);
			 // vcenter 5.5 aix ssh. 
			 
			}
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 
    	 
    	
    }
    
    public static void doChangeVnc(String url, String username, String password, String vmname, String vncport) throws Exception
    
    {
        ServiceInstance si = new ServiceInstance(new URL(url), username, password, true);
        Folder rootFolder = si.getRootFolder();
        VirtualMachine vm = null;
     
        vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
     
        if(vm==null)
        {
            System.out.println("VM not found");
            return;
        }
        
        
        
     
        VirtualMachineConfigSpec vms = new VirtualMachineConfigSpec();
        OptionValue opt = new OptionValue();
        opt.setKey("remotedisplay.vnc.port");
        opt.setValue(vncport);
        OptionValue opt2 = new OptionValue();
        opt2.setKey("remotedisplay.vnc.enabled");
        opt2.setValue("TRUE");
     
        vms.extraConfig = new OptionValue[] { opt, opt2 };
        Task task = vm.reconfigVM_Task(vms);
        String result = task.waitForMe();
     
        System.out.println("Successfully changed VNC port on " + vmname + " to " + vncport);
    }
    
    public static void main(String[] args) throws Exception {

    ServiceInstance si = new ServiceInstance(new URL(URL_VCENTER), username,
            password, true);

    String hostname = "10.108.30.12"; // this is the ip address of esx-i server
    Folder rootFolder = si.getRootFolder();
    HostSystem host = null;
    host = (HostSystem) new InventoryNavigator(rootFolder)
            .searchManagedEntity("HostSystem", hostname);

    if (host == null) {
        System.out.println("Cannot find the host:" + hostname);
        si.getServerConnection().logout();
        return;
    }

    HostSnmpSystem hss = host.getHostSnmpSystem();

    HostSnmpConfigSpec spec = new HostSnmpConfigSpec();
    spec.setEnabled(true);
    spec.setReadOnlyCommunities(new String[] { "visdk" });
    HostSnmpDestination dest = new HostSnmpDestination();
    dest.setCommunity("visdk");
    dest.setHostName("10.108.67.46");
    dest.setPort(162);
    spec.setTrapTargets(new HostSnmpDestination[] { dest });

    hss.reconfigureSnmpAgent(spec);
    si.getServerConnection().logout();
}
   
}